package com.diandi.messages.controller.phone;


import com.diandi.messages.bean.phone.MessageMsg;

import com.diandi.messages.enums.ReturnCode;
import com.diandi.messages.service.phone.MessageMsgService;
import com.diandi.messages.tools.CreateTradeNo;
import com.diandi.messages.tools.Tools;
import com.diandi.messages.utils.http.CheckParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 短信通道转发控制器
* @author mwz
* @create 2017-09-15 16:04
* @version V1.0.0
*/
@Controller
@RequestMapping("/MessageMsg")
public class MessageMsgController {
    private static Logger logger = LoggerFactory.getLogger(MessageMsgController.class);

    @Autowired
    MessageMsgService messageMsgService;


    /**
     * 查询短信发送信息
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/getMessageMsg")
    @ResponseBody
    public String getMessageMsg(HttpServletRequest request, HttpServletResponse response){
        String platTradeNo = CreateTradeNo.createTradeNo("TR");
        logger.info("--platTradeNo:"+platTradeNo+":消息平台{短信发送}开始处理--");
        Map<String,String> returnMap =  new HashMap<String,String>();
        Map<String, String> requestMap = Tools.getParameterMap(request, false);
        logger.info("--收到业务平台[CA申请认证]请求消息requestMap:{}",request.getRequestURL()+requestMap.toString());


        returnMap = CheckParams.checkParamsIsExist( requestMap, "userId","merchantNo","passCode","businessId" );
        if ( null != returnMap && !returnMap.isEmpty() ) {
            logger.info("--platTradeNo:"+platTradeNo+":消息平台{短信发送}"+returnMap.toString()+"不能为空,返回给业务端,结束处理--");
            returnMap = Tools.createReturnParams( ReturnCode.err_1004,ReturnCode.err_1004.getMsg(),null );
            return Tools.createBusinessLinkString(returnMap,true, "UTF-8");
        }
        Map<String,String> queryMap = new HashMap<String,String>();
        queryMap.put("businessId",requestMap.get("businessId"));
        MessageMsg messageMsg = messageMsgService.getMessageMsg(queryMap);
        if(messageMsg!=null){


        }
        logger.info("--platTradeNo:"+platTradeNo+":消息平台{短信发送}开始处理--");
        return   Tools.createBusinessLinkString(returnMap,true, "UTF-8");

    }

    /**
     * 发送短信信息
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/sendMessageMsg")
    @ResponseBody
    public String sendMessageMsg(HttpServletRequest request, HttpServletResponse response){
        Map<String,String> returnMap =  new HashMap<String,String>();
        String platTradeNo = CreateTradeNo.createTradeNo("TR");
        logger.info("--platTradeNo:"+platTradeNo+":消息平台{短信发送}开始处理--");
        Map<String, String> requestMap = Tools.getParameterMap(request, false);
        logger.info("--platTradeNo:"+platTradeNo+":消息平台接收到消息："+requestMap.toString()+"--");
        returnMap = CheckParams.checkParamsIsExist( requestMap, "merchantNo","msgList" );
        if ( null != returnMap && !returnMap.isEmpty() ) {
            logger.info("--platTradeNo:"+platTradeNo+":消息平台{短信发送}"+returnMap.toString()+"不能为空,返回给业务端,结束处理--");
            returnMap = Tools.createReturnParams( ReturnCode.err_1004,ReturnCode.err_1004.getMsg(),null );
            return Tools.createBusinessLinkString(returnMap,true, "UTF-8");
        }
        returnMap = messageMsgService.sendMessages(request,platTradeNo );
        logger.info("--platTradeNo:"+platTradeNo+":消息平台{短信短信},返回结果："+returnMap.toString()+"+束处理--");
        return Tools.createBusinessLinkString(returnMap,true, "UTF-8");
    }


}
